﻿using System;
using System.IO;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Terraria.GameContent.UI.Elements;
using Terraria.Graphics;
using Terraria.Localization;
using Terraria.UI;

namespace Terraria.ModLoader.UI
{
	// Token: 0x020002A6 RID: 678
	internal class UIModPackItem : UIPanel
	{
		// Token: 0x06001B06 RID: 6918 RVA: 0x0042DCB4 File Offset: 0x0042BEB4
		public UIModPackItem(string name, string[] mods)
		{
			this.filename = name;
			this.mods = mods;
			this.numMods = mods.Length;
			this.modMissing = new bool[mods.Length];
			this.numModsEnabled = 0;
			this.numModsDisabled = 0;
			this.numModsMissing = 0;
			for (int i = 0; i < mods.Length; i++)
			{
				if (UIModPacks.mods.Contains(mods[i]))
				{
					if (ModLoader.IsEnabled(mods[i]))
					{
						this.numModsEnabled++;
					}
					else
					{
						this.numModsDisabled++;
					}
				}
				else
				{
					this.modMissing[i] = true;
					this.numModsMissing++;
				}
			}
			this.BorderColor = new Color(89, 116, 213) * 0.7f;
			this.dividerTexture = TextureManager.Load("Images/UI/Divider");
			this.innerPanelTexture = TextureManager.Load("Images/UI/InnerPanelBackground");
			this.Height.Set(126f, 0f);
			this.Width.Set(0f, 1f);
			base.SetPadding(6f);
			this.modName = new UIText(name, 1f, false);
			this.modName.Left.Set(10f, 0f);
			this.modName.Top.Set(5f, 0f);
			base.Append(this.modName);
			UITextPanel<string> uitextPanel = new UITextPanel<string>(Language.GetTextValue("tModLoader.ModPackViewList"), 1f, false);
			uitextPanel.Width.Set(100f, 0f);
			uitextPanel.Height.Set(30f, 0f);
			uitextPanel.Left.Set(430f, 0f);
			uitextPanel.Top.Set(40f, 0f);
			uitextPanel.PaddingTop -= 2f;
			uitextPanel.PaddingBottom -= 2f;
			uitextPanel.OnMouseOver += UICommon.FadedMouseOver;
			uitextPanel.OnMouseOut += UICommon.FadedMouseOut;
			uitextPanel.OnClick += UIModPackItem.ViewListInfo;
			base.Append(uitextPanel);
			this.enableListButton = new UITextPanel<string>(Language.GetTextValue("tModLoader.ModPackEnableThisList"), 1f, false);
			this.enableListButton.Width.Set(100f, 0f);
			this.enableListButton.Height.Set(30f, 0f);
			this.enableListButton.Left.Set(273f, 0f);
			this.enableListButton.Top.Set(40f, 0f);
			this.enableListButton.PaddingTop -= 2f;
			this.enableListButton.PaddingBottom -= 2f;
			this.enableListButton.OnMouseOver += UICommon.FadedMouseOver;
			this.enableListButton.OnMouseOut += UICommon.FadedMouseOut;
			this.enableListButton.OnClick += UIModPackItem.EnableList;
			base.Append(this.enableListButton);
			this.enableListOnlyButton = new UITextPanel<string>(Language.GetTextValue("tModLoader.ModPackEnableOnlyThisList"), 1f, false);
			this.enableListOnlyButton.Width.Set(100f, 0f);
			this.enableListOnlyButton.Height.Set(30f, 0f);
			this.enableListOnlyButton.Left.Set(75f, 0f);
			this.enableListOnlyButton.Top.Set(40f, 0f);
			this.enableListOnlyButton.PaddingTop -= 2f;
			this.enableListOnlyButton.PaddingBottom -= 2f;
			this.enableListOnlyButton.OnMouseOver += UICommon.FadedMouseOver;
			this.enableListOnlyButton.OnMouseOut += UICommon.FadedMouseOut;
			this.enableListOnlyButton.OnClick += UIModPackItem.EnableListOnly;
			base.Append(this.enableListOnlyButton);
			this.viewInModBrowserButton = new UITextPanel<string>(Language.GetTextValue("tModLoader.ModPackViewModsInModBrowser"), 1f, false);
			this.viewInModBrowserButton.Width.Set(50f, 0f);
			this.viewInModBrowserButton.Height.Set(30f, 0f);
			this.viewInModBrowserButton.Left.Set(75f, 0f);
			this.viewInModBrowserButton.Top.Set(80f, 0f);
			this.viewInModBrowserButton.PaddingTop -= 2f;
			this.viewInModBrowserButton.PaddingBottom -= 2f;
			this.viewInModBrowserButton.OnMouseOver += UICommon.FadedMouseOver;
			this.viewInModBrowserButton.OnMouseOut += UICommon.FadedMouseOut;
			this.viewInModBrowserButton.OnClick += UIModPackItem.ViewInModBrowser;
			base.Append(this.viewInModBrowserButton);
			this.deleteButton = new UIImageButton(TextureManager.Load("Images/UI/ButtonDelete"));
			this.deleteButton.Top.Set(40f, 0f);
			this.deleteButton.OnClick += this.DeleteButtonClick;
			base.Append(this.deleteButton);
		}

		// Token: 0x06001B07 RID: 6919 RVA: 0x0042E248 File Offset: 0x0042C448
		private void DrawPanel(SpriteBatch spriteBatch, Vector2 position, float width)
		{
			spriteBatch.Draw(this.innerPanelTexture, position, new Rectangle?(new Rectangle(0, 0, 8, this.innerPanelTexture.Height)), Color.White);
			spriteBatch.Draw(this.innerPanelTexture, new Vector2(position.X + 8f, position.Y), new Rectangle?(new Rectangle(8, 0, 8, this.innerPanelTexture.Height)), Color.White, 0f, Vector2.Zero, new Vector2((width - 16f) / 8f, 1f), SpriteEffects.None, 0f);
			spriteBatch.Draw(this.innerPanelTexture, new Vector2(position.X + width - 8f, position.Y), new Rectangle?(new Rectangle(16, 0, 8, this.innerPanelTexture.Height)), Color.White);
		}

		// Token: 0x06001B08 RID: 6920 RVA: 0x0042E32C File Offset: 0x0042C52C
		private void DrawEnabledText(SpriteBatch spriteBatch, Vector2 drawPos)
		{
			string textValue = Language.GetTextValue("tModLoader.ModPackModsAvailableStatus", new object[]
			{
				this.numMods,
				this.numModsEnabled,
				this.numModsDisabled,
				this.numModsMissing
			});
			Color color = (this.numModsMissing > 0) ? Color.Red : ((this.numModsDisabled > 0) ? Color.Yellow : Color.Green);
			Utils.DrawBorderString(spriteBatch, textValue, drawPos, color, 1f, 0f, 0f, -1);
		}

		// Token: 0x06001B09 RID: 6921 RVA: 0x0042E3C4 File Offset: 0x0042C5C4
		protected override void DrawSelf(SpriteBatch spriteBatch)
		{
			base.DrawSelf(spriteBatch);
			CalculatedStyle innerDimensions = base.GetInnerDimensions();
			Vector2 vector = new Vector2(innerDimensions.X + 5f, innerDimensions.Y + 30f);
			spriteBatch.Draw(this.dividerTexture, vector, null, Color.White, 0f, Vector2.Zero, new Vector2((innerDimensions.Width - 10f) / 8f, 1f), SpriteEffects.None, 0f);
			vector = new Vector2(innerDimensions.X + innerDimensions.Width - 355f, innerDimensions.Y);
			this.DrawPanel(spriteBatch, vector, 350f);
			this.DrawEnabledText(spriteBatch, vector + new Vector2(15f, 5f));
		}

		// Token: 0x06001B0A RID: 6922 RVA: 0x000132A0 File Offset: 0x000114A0
		public override void MouseOver(UIMouseEvent evt)
		{
			base.MouseOver(evt);
			this.BackgroundColor = new Color(73, 94, 171);
			this.BorderColor = new Color(89, 116, 213);
		}

		// Token: 0x06001B0B RID: 6923 RVA: 0x0042C064 File Offset: 0x0042A264
		public override void MouseOut(UIMouseEvent evt)
		{
			base.MouseOut(evt);
			this.BackgroundColor = new Color(63, 82, 151) * 0.7f;
			this.BorderColor = new Color(89, 116, 213) * 0.7f;
		}

		// Token: 0x06001B0C RID: 6924 RVA: 0x0042E490 File Offset: 0x0042C690
		private void DeleteButtonClick(UIMouseEvent evt, UIElement listeningElement)
		{
			UIModPackItem uimodPackItem = (UIModPackItem)listeningElement.Parent;
			Directory.CreateDirectory(UIModPacks.ModListSaveDirectory);
			string path = UIModPacks.ModListSaveDirectory + Path.DirectorySeparatorChar.ToString() + uimodPackItem.filename + ".json";
			if (File.Exists(path))
			{
				File.Delete(path);
			}
			Main.menuMode = 10016;
		}

		// Token: 0x06001B0D RID: 6925 RVA: 0x0042E4F0 File Offset: 0x0042C6F0
		private static void EnableList(UIMouseEvent evt, UIElement listeningElement)
		{
			UIModPackItem uimodPackItem = (UIModPackItem)listeningElement.Parent;
			foreach (string value in uimodPackItem.mods)
			{
				if (UIModPacks.mods.Contains(value))
				{
					ModLoader.EnableMod(value);
				}
			}
			Main.menuMode = 10016;
			if (uimodPackItem.numModsMissing > 0)
			{
				string text = "";
				for (int j = 0; j < uimodPackItem.mods.Length; j++)
				{
					if (uimodPackItem.modMissing[j])
					{
						text = text + uimodPackItem.mods[j] + "\n";
					}
				}
				Interface.infoMessage.SetMessage(Language.GetTextValue("tModLoader.ModPackModsMissing", text));
				Interface.infoMessage.SetGotoMenu(10016, null);
				Main.menuMode = 10013;
			}
		}

		// Token: 0x06001B0E RID: 6926 RVA: 0x0042E5B8 File Offset: 0x0042C7B8
		private static void ViewInModBrowser(UIMouseEvent evt, UIElement listeningElement)
		{
			UIModPackItem uimodPackItem = (UIModPackItem)listeningElement.Parent;
			Interface.modBrowser.Activate();
			Interface.modBrowser.filterTextBox.currentString = "";
			Interface.modBrowser.SpecialModPackFilter = uimodPackItem.mods.ToList<string>();
			Interface.modBrowser.SpecialModPackFilterTitle = Language.GetTextValue("tModLoader.MBFilterModlist");
			Interface.modBrowser.updateFilterMode = UpdateFilter.All;
			Interface.modBrowser.modSideFilterMode = ModSideFilter.All;
			Interface.modBrowser.UpdateFilterToggle.setCurrentState((int)Interface.modBrowser.updateFilterMode);
			Interface.modBrowser.ModSideFilterToggle.setCurrentState((int)Interface.modBrowser.modSideFilterMode);
			Interface.modBrowser.updateNeeded = true;
			Main.PlaySound(10, -1, -1, 1, 1f, 0f);
			Main.menuMode = 10007;
		}

		// Token: 0x06001B0F RID: 6927 RVA: 0x0042E68C File Offset: 0x0042C88C
		private static void EnableListOnly(UIMouseEvent evt, UIElement listeningElement)
		{
			UIModPackItem uimodPackItem = (UIModPackItem)listeningElement.Parent;
			foreach (string text in UIModPacks.mods)
			{
				ModLoader.DisableMod(text);
			}
			foreach (string value in uimodPackItem.mods)
			{
				if (UIModPacks.mods.Contains(value))
				{
					ModLoader.EnableMod(value);
				}
			}
			Main.menuMode = 10006;
			if (uimodPackItem.numModsMissing > 0)
			{
				string text2 = "";
				for (int k = 0; k < uimodPackItem.mods.Length; k++)
				{
					if (uimodPackItem.modMissing[k])
					{
						text2 = text2 + uimodPackItem.mods[k] + "\n";
					}
				}
				Interface.infoMessage.SetMessage(Language.GetTextValue("tModLoader.ModPackModsMissing", text2));
				Interface.infoMessage.SetGotoMenu(10006, null);
				Main.menuMode = 10013;
			}
		}

		// Token: 0x06001B10 RID: 6928 RVA: 0x0042E780 File Offset: 0x0042C980
		private static void ViewListInfo(UIMouseEvent evt, UIElement listeningElement)
		{
			UIModPackItem uimodPackItem = (UIModPackItem)listeningElement.Parent;
			Main.PlaySound(10, -1, -1, 1, 1f, 0f);
			string text = "";
			for (int i = 0; i < uimodPackItem.mods.Length; i++)
			{
				text = text + uimodPackItem.mods[i] + (uimodPackItem.modMissing[i] ? Language.GetTextValue("tModLoader.ModPackMissing") : "") + "\n";
			}
			Interface.infoMessage.SetMessage(Language.GetTextValue("tModLoader.ModPackModsContained", text));
			Interface.infoMessage.SetGotoMenu(10016, null);
			Main.menuMode = 10013;
		}

		// Token: 0x04001834 RID: 6196
		private readonly Texture2D dividerTexture;

		// Token: 0x04001835 RID: 6197
		private readonly Texture2D innerPanelTexture;

		// Token: 0x04001836 RID: 6198
		private readonly UIText modName;

		// Token: 0x04001837 RID: 6199
		private readonly string[] mods;

		// Token: 0x04001838 RID: 6200
		private readonly bool[] modMissing;

		// Token: 0x04001839 RID: 6201
		private readonly int numMods;

		// Token: 0x0400183A RID: 6202
		private readonly int numModsEnabled;

		// Token: 0x0400183B RID: 6203
		private readonly int numModsDisabled;

		// Token: 0x0400183C RID: 6204
		private readonly int numModsMissing;

		// Token: 0x0400183D RID: 6205
		private readonly UITextPanel<string> enableListButton;

		// Token: 0x0400183E RID: 6206
		private readonly UITextPanel<string> enableListOnlyButton;

		// Token: 0x0400183F RID: 6207
		private readonly UITextPanel<string> viewInModBrowserButton;

		// Token: 0x04001840 RID: 6208
		private readonly UIImageButton deleteButton;

		// Token: 0x04001841 RID: 6209
		private readonly string filename;
	}
}
